package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.JoinAdapter;
import com.jxwifi.cloud.quickclean.adapter.PopAreaAdapter;
import com.jxwifi.cloud.quickclean.bean.AssemRecordsBean;
import com.jxwifi.cloud.quickclean.utils.TokenTimeUtils;
import com.popwindow.BasicPopmodule;

import java.util.List;

import butterknife.internal.DebouncingOnClickListener;

/**
 * 参与拼单弹框
 */
public class PopAssembleJoin extends BasicPopmodule {

    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private PopJoinListener mPoplistener;
    private List<String> mListAssem;
    private JoinAdapter mAdapter;
    private String mCdesk;
    private String mStrTitle;
    private String distanceNumber;
    private long mSeconds;

    public PopAssembleJoin(Context context,List<String> list,String cdesk,String strTitle,long mSeconds,String distanceNumber,PopJoinListener l){
        this.mContext=context;
        this.mListAssem=list;
        this.mCdesk=cdesk;
        this.mStrTitle=strTitle;
        this.mSeconds=mSeconds;
        this.mPoplistener=l;
        this.distanceNumber = distanceNumber;
        initView();
    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_assem_join, new LinearLayout(mContext), false);
        setListener();
    }

    public void setListener(){
        View mVwPopBlan = (View) mContentView.findViewById(R.id.vw_pop_blank);
        TextView mTvPopCancel = (TextView) mContentView.findViewById(R.id.tv_pop_assem_cancel);
        TextView mTvPopTitle = (TextView) mContentView.findViewById(R.id.tv_assem_type_title);
        final TextView mTvAssemJoinTime=(TextView)mContentView.findViewById(R.id.tv_assem_join_time);
        RecyclerView mTvRecyler=(RecyclerView)mContentView.findViewById(R.id.rv_pop_assem_list);
        mTvPopTitle.setText("参与"+mStrTitle+"的拼单");
        new CountDownTimer(mSeconds , 1000) {
            public void onTick(long l) {
                mTvAssemJoinTime.setText("仅剩"+distanceNumber+"个名额,"+ TokenTimeUtils.getSecondToDayHourMinutes(l)+"后结束");
            }

            public void onFinish() {
                //倒计时结束
//                holder.timeTv.setText("00:00");
            }
        }.start();
        mTvRecyler.setLayoutManager(new GridLayoutManager(mContext, 7));
        mTvRecyler.setAdapter(mAdapter = new JoinAdapter(R.layout.pop_join_list_item));
        //Log.i(TAG,"setListener ==> mListAssem.size() = "+mListAssem.size());
        mAdapter.setNewData(mListAssem);
        TextView mTvJoinNow=(TextView)mContentView.findViewById(R.id.tv_join_now);

        mVwPopBlan.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });

        mTvJoinNow.setOnClickListener(new DebouncingOnClickListener() {
            @Override
            public void doClick(View view) {
                mPoplistener.AssemJoin(mCdesk);
            }
        });
        mTvPopCancel.setOnClickListener(new DebouncingOnClickListener() {
            @Override
            public void doClick(View view) {
                mPopwindows.dismiss();
            }
        });
    }


    public interface PopJoinListener {
        void AssemJoin(String Orderid);
    }
}
